<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController {

    //显示用户详情+用户提问列表+用户回答列表
    public function UserInfo(){

        $user=M('user');
        $user_id = session('systemUserId');
        //dump($user_id);
        $this -> UserInfo = $user -> select($user_id);

        //显示提题列表
        $question=M('question');
        $this -> QuestionList = $question
            ->join('user on user.u_id=question.q_u_id')->where("q_u_id=$user_id")->select();

        //显示用户回答列表
        $answer=M('answer');
        $this -> AnswerList = $answer
            ->join('question on question.q_id=answer.an_q_id')
            ->join('user on user.u_id=answer.an_u_id')->where("an_u_id=$user_id")->select();

        //dump($this -> QuestionInfo);

        $this -> display();
    }

    //显示修改个人信息界面
    public function UserMod()
    {

        $user=M('user');
        $user_id = session('systemUserId');
        //dump($user_id);
        $this -> UserInfo = $user -> select($user_id);

        $this->display();

    }

    //修改问题
    public function doUserMod()
    {

        $user=M('user');
        $user_id = session('systemUserId');

        $UserInfo = $user -> select($user_id);

        //$user_pwd = $UserInfo['0']['u_pwd'];

        if (!$user->create()) {
//             如果创建失败 表示验证没有通过 输出错误提示信息
            exit($user->getError());
        } else {
            // 创建成功，做其他事情
            if ($user->create()) {
                //判断是否上传了新的图片
                if ($_FILES['file']['error'] == 0) { //如果有文件上传
                    //删除之前的文件
                    unlink('./Public/upload/userpics/' . $UserInfo['0']['u_pic']);
                    //上传新的文件
                    $pname = 'default.jpg';
                    if ($picinfo = $this->upload()) {
                        $pname = $picinfo['file']['savename'];
                    }
                    $user->u_pic = $pname;
                }
                $user->u_id = $user_id;
                $user->u_pwd = $UserInfo['0']['u_pwd'];
                //保存提问时间
                //$user->u_time = date("Y-m-d H:i:s",time());

                if ($user->save() !== false) {
                    $this->success('修改成功', './userinfo', 3);
                } else {
                    $this->error("修改失败");
                }
            }
        }
    }


    //答案图片 文件上传upload函数
    public function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/userpics/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $upload->saveName = time() . mt_rand(); //实际保存文件名：时间戳+随机数
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
//            $this->success('上传成功！');
            return $info;
        }
    }









}